package com.hb0730.boot.admin.modules.sci.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hb0730.boot.admin.core.service.BaseServiceImpl;
import com.hb0730.boot.admin.modules.sci.mapper.SciAccountMapper;
import com.hb0730.boot.admin.modules.sci.model.entity.SciAccount;
import com.hb0730.boot.admin.modules.sci.model.query.SciAccountTree;
import com.hb0730.boot.admin.modules.sci.util.TypeTableUtil;
import jakarta.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class SciAccountService extends BaseServiceImpl<SciAccountMapper, SciAccount> {

    LambdaQueryWrapper<SciAccount> queryWrapper = Wrappers.lambdaQuery(SciAccount.class);
    public Optional<List<SciAccount>> listAccount(String type) {
        if (StrUtil.isNotBlank(type)) {
            System.out.println(type);
            queryWrapper.orderByAsc(SciAccount::getRank).eq(SciAccount::getType, type);
        }
        return Optional.ofNullable(this.list(queryWrapper));
    }

    public List<SciAccountTree> buildTree(@Nonnull List<SciAccount> accounts) {
        List<SciAccountTree> accountTrees = BeanUtil.copyToList(accounts, SciAccountTree.class);
        List<SciAccountTree> trees = new ArrayList<>();
        Set<String> ids = new HashSet<>();
        for (SciAccountTree accountTree : accountTrees) {
            if (StrUtil.isBlank(accountTree.getParentId())) {
                trees.add(accountTree);
            }
            for (SciAccountTree tree : accountTrees) {
                if (accountTree.getId().equals(tree.getParentId())) {
                    if (accountTree.getChildren() == null) {
                        accountTree.setChildren(new ArrayList<>());
                    }
                    accountTree.getChildren().add(tree);
                    ids.add(tree.getId());
                }
            }
        }
        if (trees.size() == 0) {
            trees = accountTrees.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
        }
        return trees;
    }


    public Map<String, Object> getTime() {
        return this.baseMapper.getTime();
    }

    public int updateTime(Map<String, Object> timeAccount) {

        return this.baseMapper.updateTime(timeAccount);
    }

    public boolean checkTime() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 使用 DateTimeFormatter 格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = now.format(formatter);
        return this.baseMapper.checkTime(formattedTime).orElse(false);
    }
}
